{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]17779/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","excerpt":"문제 재현시의 시장 구재현은 지난 몇 년간 게리맨더링을 통해서 자신의 당에게 유리하게 선거구를 획정했다. 견제할 권력이 없어진 구재현은 권력을 매우 부당하게 행사했고, 심지어는 시의 이름도 재현시로 변경했다. 이번 선거에서는 최대한 공평하게 선거구를 획정하려고 한다. 재현시는 크기가 N×N인 격자로 나타낼 수 있다. 격자의 각 칸은 구역을 의미하고, r행…","html":"<h2>문제</h2>\n<p>재현시의 시장 구재현은 지난 몇 년간 게리맨더링을 통해서 자신의 당에게 유리하게 선거구를 획정했다. 견제할 권력이 없어진 구재현은 권력을 매우 부당하게 행사했고, 심지어는 시의 이름도 재현시로 변경했다. 이번 선거에서는 최대한 공평하게 선거구를 획정하려고 한다.</p>\n<p>재현시는 크기가 N×N인 격자로 나타낼 수 있다. 격자의 각 칸은 구역을 의미하고, r행 c열에 있는 구역은 (r, c)로 나타낼 수 있다. 구역을 다섯 개의 선거구로 나눠야 하고, 각 구역은 다섯 선거구 중 하나에 포함되어야 한다. 선거구는 구역을 적어도 하나 포함해야 하고, 한 선거구에 포함되어 있는 구역은 모두 연결되어 있어야 한다. 구역 A에서 인접한 구역을 통해서 구역 B로 갈 수 있을 때, 두 구역은 연결되어 있다고 한다. 중간에 통하는 인접한 구역은 0개 이상이어야 하고, 모두 같은 선거구에 포함된 구역이어야 한다.</p>\n<p>선거구를 나누는 방법은 다음과 같다.</p>\n<ol>\n<li>기준점 (x, y)와 경계의 길이 d1, d2를 정한다. (d1, d2 ≥ 1, 1 ≤ x &#x3C; x+d1+d2 ≤ N, 1 ≤ y-d1 &#x3C; y &#x3C; y+d2 ≤ N)</li>\n<li>\n<p>다음 칸은 경계선이다.</p>\n<ol>\n<li>(x, y), (x+1, y-1), ..., (x+d1, y-d1)</li>\n<li>(x, y), (x+1, y+1), ..., (x+d2, y+d2)</li>\n<li>(x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)</li>\n<li>(x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)</li>\n</ol>\n</li>\n<li>경계선과 경계선의 안에 포함되어있는 곳은 5번 선거구이다.</li>\n<li>\n<p>5번 선거구에 포함되지 않은 구역 (r, c)의 선거구 번호는 다음 기준을 따른다.</p>\n<ol>\n<li>1번 선거구: 1 ≤ r &#x3C; x+d1, 1 ≤ c ≤ y</li>\n<li>2번 선거구: 1 ≤ r ≤ x+d2, y &#x3C; c ≤ N</li>\n<li>3번 선거구: x+d1 ≤ r ≤ N, 1 ≤ c &#x3C; y-d1+d2</li>\n<li>4번 선거구: x+d2 &#x3C; r ≤ N, y-d1+d2 ≤ c ≤ N</li>\n</ol>\n</li>\n</ol>\n<p>아래는 크기가 7×7인 재현시를 다섯 개의 선거구로 나눈 방법의 예시이다.</p>\n<img src='https://upload.acmicpc.net/813c38e0-3197-4589-bc96-17d96eb9ed14/-/preview/' width='80%'>\n<p>구역 (r, c)의 인구는 A[r][c]이고, 선거구의 인구는 선거구에 포함된 구역의 인구를 모두 합한 값이다. 선거구를 나누는 방법 중에서, 인구가 가장 많은 선거구와 가장 적은 선거구의 인구 차이의 최솟값을 구해보자.</p>\n<h3>입력</h3>\n<ul>\n<li>첫째 줄에 재현시의 크기 N이 주어진다.</li>\n<li>둘째 줄부터 N개의 줄에 N개의 정수가 주어진다. r행 c열의 정수는 A[r][c]를 의미한다.</li>\n</ul>\n<h3>출력</h3>\n<ul>\n<li>첫째 줄에 인구가 가장 많은 선거구와 가장 적은 선거구의 인구 차이의 최솟값을 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Dosi {\n    int N;\n    int minDiff = Integer.MAX_VALUE;\n    int[][] map;\n    int[][] copyMap;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n    ...\n\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>선거구의 정보를 담당하는 클래스이다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public int vote() {...}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>주어진 기준에 따라 선거구를 나누고, 인구 수를 합산하는 로직을 실행하는 메소드이다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void setLine(int x, int y, int d1, int d2) {...}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>5번 선거구의 가장자리를 긋고, 내부 영역을 탐색하는 <code class=\"language-text\">isConnected</code> 를 실행하는 메소드이다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void isConnected(int x, int y) {...}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>재귀적으로 5번 선거구의 내부를 탐색한다.</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.*;\n\nclass Dosi {\n    int N;\n    int minDiff = Integer.MAX_VALUE;\n    int[][] map;\n    int[][] copyMap;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n\n    public Dosi(int N) {\n        this.N = N;\n        map = new int[N + 1][N + 1];\n    }\n\n    public int vote() {\n        for (int d1 = 1; d1 &lt;= N; d1++) {\n            for (int d2 = 1; d2 &lt;= N; d2++) {\n                for (int x = 1; x &lt;= N; x++) {\n                    for (int y = 1; y &lt;= N; y++) {\n                        if (!(x + d1 + d2 &lt;= N &amp;&amp; y - d1 &gt;= 1 &amp;&amp; y + d2 &lt;= N)) continue;\n                        copyMap = new int[N + 1][N + 1];\n                        popSum1(x, y, d1);\n                        popSum2(x, y ,d2);\n                        popSum3(x, y, d1, d2);\n                        popSum4(x, y, d1, d2);\n                        setLine(x, y, d1, d2);\n                        sum();\n                    }\n                }\n            }\n        }\n        return minDiff;\n    }\n\n    private void sum() {\n        int max = 0;\n        int min = Integer.MAX_VALUE;\n        int[] popSum = new int[6];\n        for(int i=1; i&lt;=N; i++) {\n            for(int j=1; j&lt;=N; j++) {\n                popSum[copyMap[i][j]]+=map[i][j];\n            }\n        }\n        for(int i=1; i&lt;=5; i++) {\n            max = Math.max(max, popSum[i]);\n            min = Math.min(min, popSum[i]);\n        }\n        minDiff = Math.min(minDiff, max-min);\n    }\n\n    private void setLine(int x, int y, int d1, int d2) {\n        for(int i = 0; i &lt;= d1; i++) {\n            copyMap[x+i][y-i] = 5;\n        }\n        for(int i = 0; i &lt;= d2; i++) {\n            copyMap[x+i][y+i] = 5;\n        }\n        for(int i = 0; i &lt;= d2; i++) {\n            copyMap[x+d1+i][y-d1+i] = 5;\n        }\n        for(int i = 0; i &lt;= d1; i++) {\n            copyMap[x+d2+i][y+d2-i] = 5;\n        }\n        for(int alpha = 0; alpha &lt; d1; alpha++) {\n            isConnected(x+alpha+1, y-alpha);\n        }\n        for(int alpha = 0; alpha &lt; d2; alpha++) {\n            isConnected(x+alpha+1, y+alpha);\n        }\n    }\n\n    private void isConnected(int x, int y) {\n        copyMap[x][y] = 5;\n\n        for(int i=0; i&lt;4; i++) {\n            int nx = x + rangeX[i];\n            int ny = y + rangeY[i];\n            if (nx &lt; 1 || nx &gt; N || ny &lt; 1 || ny &gt; N) continue;\n            if (copyMap[nx][ny] != 5) isConnected(nx,ny);\n        }\n    }\n\n    private void popSum1(int x, int y, int d1) {\n        for (int i = 1; i &lt; x + d1; i++) {\n            for (int j = 1; j &lt;= y; j++) {\n                copyMap[i][j] = 1;\n            }\n        }\n    }\n\n    private void popSum2(int x, int y, int d2) {\n        for (int i = 1; i &lt;= x + d2; i++) {\n            for (int j = y + 1; j &lt;= N; j++) {\n                copyMap[i][j] = 2;\n            }\n        }\n    }\n\n    private void popSum3(int x, int y, int d1, int d2) {\n        for (int i = x + d1; i &lt;= N; i++) {\n            for (int j = 1; j &lt; y - d1 + d2; j++) {\n                copyMap[i][j] = 3;\n            }\n        }\n    }\n\n    private void popSum4(int x, int y, int d1, int d2) {\n        for (int i = x + d2 + 1; i &lt;= N; i++) {\n            for (int j = y - d1 + d2; j &lt;= N; j++) {\n                copyMap[i][j] = 4;\n            }\n        }\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        int N = Integer.parseInt(br.readLine());\n\n        Dosi dosi = new Dosi(N);\n        String[] line;\n\n        for (int i = 1; i &lt;= N; i++) {\n            line = br.readLine().split(&quot; &quot;);\n            for (int j = 1; j &lt;= N; j++) {\n                dosi.map[i][j] = Integer.parseInt(line[j - 1]);\n            }\n        }\n\n        System.out.println(dosi.vote());\n\n        br.close();\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>처음에는 게리맨더링이랑 비슷한 줄 알았는데 조금은 달라서 생각해보는데 시간이 좀 걸렸다. </li>\n<li>\n<p>선거구를 나누는 로직을 구현하는데 시간이 걸렸고, 게시판을 참고해서 나름대로 풀어봤는데, 최종적으로 인구수를 합산해 차이의 최소를 구하는 로직이 자꾸 틀렸다.</p>\n<ul>\n<li>어차피 나눠진 선거구의 인구 수를 나눌 때마다 합산하고 선거구를 다 나눈 뒤 (즉, 인구 수 합산까지 완료된 상태) 차이를 구하고 이를 업데이트하는 방식은 왜 틀리는지 모르겠다.</li>\n</ul>\n</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/17779\">https://www.acmicpc.net/problem/17779</a></p>","frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2","date":"March 20, 2022","update":"March 20, 2022","tags":["구현","브루트포스","시뮬레이션"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]17779/","readingTime":{"minutes":8.4}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[Java지식]얕은복사&깊은복사/"},"frontmatter":{"title":"📃 얕은 복사와 깊은 복사"}},"next":{"fields":{"slug":"/[Java지식]HashMap&LinkedHashMap/"},"frontmatter":{"title":"📃 HashMap과 LinkedHashMap"}}},"pageContext":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","series":"👩‍💻 BOJ","previousPostId":"92c9220c-c4b0-59a2-a626-5a22f979bf39","nextPostId":"ccb919b8-dc72-524a-8a71-a85c4554249f"}},"staticQueryHashes":[]}